layui.config({
    base: basePath,
    version: skyeyeVersion
}).extend({
    window: 'js/winui.window'
}).define(['window', 'jquery', 'winui'], function (exports) {
    winui.renderColor();
    layui.use(['form', 'laydate', 'dtree'], function (form) {
        var index = parent.layer.getFrameIndex(window.name);
        var $ = layui.$,
            form = layui.form,
            laydate = layui.laydate,
            dtree = layui.dtree;

        // 全局变量定义
        var companyId = "";
        var departmentId = "";
        var jobId = "";
        var jobScoreId = "";

        // 工种类型
        $(".hourlyPriceBox").hide();
        skyeyeClassEnumUtil.showEnumDataListByClassName("userStaffWorkstationType", 'radio', "userStaffWorkstationType", '', form);
        form.on('radio(userStaffWorkstationTypeFilter)', function (data) {
            let val = data.value;
            if (val == 2) {
                // 小时工
                $(".hourlyPriceBox").show();
            } else {
                $(".hourlyPriceBox").hide();
            }
        });

        // 初始化参加工作时间选择器
        laydate.render({
            elem: '#workTime',
            type: 'date',
            format: 'yyyy-MM-dd',
            trigger: 'click',
            done: function (value, date) {
                // 当选择了参加工作时间后，限制入职时间不能晚于参加工作时间
                var workDate = new Date(value);
                var entryDate = new Date($("#entryTime").val());
                if (entryDate < workDate || entryDate === workDate) {
                    $("#workTime").val('');
                    winui.window.msg('参加工作时间不能晚于入职时间', { icon: 2, time: 2000 });
                }
            }
        });

        // 初始化入职时间选择器
        laydate.render({
            elem: '#entryTime',
            type: 'date',
            format: 'yyyy-MM-dd',
            trigger: 'click',
            done: function (value, date) {
                // 当选择了入职时间后，检查是否早于参加工作时间
                var entryDate = new Date(value);
                var workDate = new Date($("#workTime").val());
                if (entryDate < workDate || entryDate === workDate) {
                    $("#entryTime").val('');
                    winui.window.msg('入职时间不能早于参加工作时间', { icon: 2, time: 2000 });
                }
            }
        });

        // 初始化试用期时间选择器
        laydate.render({
            elem: '#trialTime',
            type: 'date',
            format: 'yyyy-MM-dd',
            min: 0, // 最小日期为今天
            trigger: 'click'
        });

        // 监听员工状态选择事件
        form.on('select(stateFilter)', function (data) {
            if (data.value == 4) { // 试用期状态
                $("#trialTimeBox").show();
            } else {
                $("#trialTimeBox").hide();
            }
        });

        // 初始化公司树
        dtree.render({
            elem: "#companyTree",
            url: reqBasePath + 'queryCompanyMationListTree',
            dataStyle: 'layuiStyle',
            done: function (data) {

            }
        });


        // 公司树点击事件
        dtree.on("node('companyTree')", function (param) {
            companyId = param.nodeId;
            // 初始化部门
            dtree.render({
                elem: "#departmentTree",
                url: reqBasePath + 'companydepartment006?companyId=' + companyId,
                dataStyle: 'layuiStyle',
                done: function (data) {
                    departmentId = "";
                }
            });
        });

        // 部门树点击事件
        dtree.on("node('departmentTree')", function (param) {
            departmentId = param.nodeId;
            //初始化职位
            dtree.render({
                elem: "#jobTree",
                url: reqBasePath + 'companyjob006?departmentId=' + departmentId,
                dataStyle: 'layuiStyle',
                done: function (data) {
                    jobId = "";
                }
            });
        });

        // 职位树点击事件
        dtree.on("node('jobTree')", function (param) {
            jobId = param.nodeId;
            // 初始化职位定级
            dtree.render({
                elem: "#jobScoreTree",
                url: reqBasePath + 'companyjobscore008?jobId=' + jobId,
                dataStyle: 'layuiStyle',
                method: 'GET',
                done: function (data) {
                    jobScoreId = "";
                }
            });
        });

        // 职位定级树点击事件
        dtree.on("node('jobScoreTree')", function (param) {
            jobScoreId = param.nodeId;
        });

        matchingLanguage();
        form.render();

        // 监听表单提交
        form.on('submit(formInvite)', function (data) {
            if (winui.verifyForm(data.elem)) {

                if (isNull(companyId)) {
                    winui.window.msg('请选择所属公司', { icon: 2, time: 2000 });
                    return false;
                }
                if (isNull(departmentId)) {
                    winui.window.msg('请选择所属部门', { icon: 2, time: 2000 });
                    return false;
                }
                if (isNull(jobId)) {
                    winui.window.msg('请选择职位信息', { icon: 2, time: 2000 });
                    return false;
                }

                // 获取状态值
                var state = $("#state").val();

                // 如果是试用期，验证试用期到期时间
                if (state == 4 && isNull($("#trialTime").val())) {
                    winui.window.msg('请选择试用期到期时间', { icon: 2, time: 2000 });
                    return false;
                }

                // 验证工种类型
                let userStaffWorkstationType = getNotUndefinedVal(dataShowType.getData('userStaffWorkstationType'));
                if (userStaffWorkstationType == 2 && isNull($("#hourlyPrice").val())) {
                    winui.window.msg('请输入小时工价', { icon: 2, time: 2000 });
                    return false;
                }

                // 构建请求参数
                var params = {
                    email: $("#email").val(),
                    phone: $("#phone").val(),
                    companyId: companyId,
                    departmentId: departmentId,
                    jobId: jobId,
                    jobScoreId: jobScoreId,
                    state: state,
                    workTime: $("#workTime").val(),
                    entryTime: $("#entryTime").val(),
                    tenantId: getTenantId(),
                    workstationType: userStaffWorkstationType,
                    hourlyPrice: $("#hourlyPrice").val() || 0
                };

                // 如果是试用期，添加试用期到期时间
                if (state == 4) {
                    params.trialTime = $("#trialTime").val();
                }

                // 发送邀请请求
                AjaxPostUtil.request({
                    url: reqBasePath + "inviteUsersToJoin",
                    params: params,
                    type: 'json',
                    callback: function (json) {
                        parent.layer.close(index);
                        parent.refreshCode = '0';
                    }
                });
            }
            return false;
        });

        // 取消按钮事件
        $("body").on("click", "#cancle", function () {
            parent.layer.close(index);
        });
    });
});
